<template>
	<view class="goodsList">
			<navigator url="/pages_manage/goods/add" class="head">
				<view class="lefticon">
					<u-icon name="plus"></u-icon>
				</view>
				<view class="text"> 
					<text>新增商品</text></view>
			</navigator>
		<view class="goodsGroup" v-for="group in goodsList" :key="group._id">
			<view class="category">
			<uni-section :title="group.name" type="line"></uni-section>
			</view>
			<view class="goodsRow" v-for="item in group.proGroup" :key="item._id" >
				<view class="goodsleft">
					<view class="goodsThumb">
						<image :src="item.thumb[0].url" mode=""></image>
						<!-- <image src="../../static/images/code.jpeg" mode=""></image> -->
					</view>
					
				</view>
				<view class="goodsInfo">
						<view class="goodsTitle">
							<text>{{ item.name }}</text>
						</view>
						<view class="goodsPrice">
							<text>￥{{ item.price }}</text>
						</view>
					</view>
				<view class="goodsright">
					<view class="icon" @click="clickEdit(item._id)">
						<u-icon name="edit-pen" size="25"></u-icon> 
					</view>
					<view class="icon" @click="clickRemove(item._id)">
						<u-icon name="trash" size="25"></u-icon>
					</view>
				</view>
			</view>
		</view>	
	</view>
</template>

<script>
	const goodsListCloudObj = uniCloud.importObject("wanshop-mall-goods")
	export default {
		data() {
			return {
				goodsList:[]
			};
		},
		onLoad() {
			this.getGoodsList();
		},
		onShow() {
			this.isManage()
			this.getGoodsList();
		},
		methods:{
			// 点击调转到修改页面
			clickEdit(id){
				console.log(id);
				uni.navigateTo({
					url:"/pages_manage/goods/add?id="+id
				})
			},
			
			// 删除某个商品
			clickRemove(id){
				uni.showModal({
					title:"是否确认删除？",
					success: res =>{
						if(res.confirm){
							this.removeGoods(id)
						}
						console.log(res);
					}
				})
				// return;
				// 
			},
			// 删除数据
			async removeGoods(id){
				let res = await goodsListCloudObj.remove(id);
				if(res.code == -1){
					uni.showToast({
						title:res.msg,
						icon:"error"
					})
					return;
				}
				console.log(res);
				this.getGoodsList();
			},
			
			// 获取商品列表
			async getGoodsList(){
				let  res = await goodsListCloudObj.getList();
				console.log(res);
				this.goodsList = res;
			}
		}
		
	}
</script>

<style lang="scss" scoped>
.goodsList{
	padding:30rpx;
	.head{
		@include flex-box-set(start);
		padding: 20rpx 0;
		border-bottom:1rpx solid $border-color-light;
		.u-icon { // 针对图标类添加样式
			margin-right: 10rpx; // 图标和文字之间的间距
		        }
	}
	.goodsGroup{
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid $border-color-light;
		.category{
			padding: 20rpx 0 ;
		}
		.goodsRow{
			padding-bottom: 20rpx;
			@include flex-box-set(start);
			align-items: flex-start;
			
			.goodsleft{
				@include flex-box();
				
				width: 180rpx;
				height: 180rpx;
				.goodsThumb{
					width: 100%;
					height: 100%;
					
					image{
						border-radius: 10rpx;
						width: 100%;
						height: 100%;
					}
				}
				
				
			}
			.goodsInfo{
				padding-left: 30rpx;
				flex:1;
				// @include flex-box-set(start);
				flex-direction: column;
				justify-content: space-around;
				text-align: left;
				.goodsTitle{
					font-size: 32rpx;
					font-weight: bold;
					margin-bottom: 30rpx;
				}
				.goodsPrice {
					font-size: 28rpx;
					color: $text-font-color-2;
				                }
			}
			.goodsright{
				height:100%;
				@include flex-box-set(end);
				flex-direction: column;
				align-items: center;
				.icon {
						
						margin-left: 10rpx; // 图标之间的间距
				        margin-bottom: 50%;        
								}
			}
	}
}

}
</style>
